package tlias.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import tlias.mapper.StudentMapper;
import tlias.pojo.*;
import tlias.pojo.StudentVO;
import tlias.service.StudentService;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class StudentServiceImpl implements StudentService {
    private final StudentMapper studentMapper;

    public StudentServiceImpl(StudentMapper studentMapper) {
        this.studentMapper = studentMapper;
    }

    @Override
    public PageResult<StudentVO> list(StudentQueryParam param) {
        PageHelper.startPage(param.getPage(), param.getPageSize());
        Page<StudentVO> page = (Page<StudentVO>) studentMapper.list(param);
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    @Override
    public void delete(Integer[] ids) {
        studentMapper.delete(ids);
    }

    @Override
    public void add(Student student) {
        student.setClazzId(student.getClazzId() == null ? 999 : student.getClazzId());
        studentMapper.add(student);
    }

    @Override
    public StudentVO getById(Integer id) {
        return studentMapper.getById(id);
    }

    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }

    @Override
    public void handleViolation(Integer id, Integer score) {
        studentMapper.updateViolation(id, score);
    }
}